def solution(distance, n, gas_stations):
    # 如果没有加油站且不能直接到达目的地，返回 Impossible
    if n == 0 and distance > 200:
        return "Impossible"

    # 将加油站按照离「景点 X」的距离从近到远排序
    gas_stations.sort(key=lambda x: x[0])
    # 统计加油站油价
    price_list = []
    for gas_station in gas_stations:
        price_list.append(gas_station[1])

    # 初始状态：起点在距离终点 0 km 的位置，初始油量 200L
    current_fuel = 200
    current_position = 0
    total_cost = 0
    max_fuel_capacity = 400

    #

    # 遍历所有加油站
    for i in range(n):
        station_distance = gas_stations[i][0]
        station_price = gas_stations[i][1]

        # 计算到下一个加油站需要行驶的距离
        distance_to_station = station_distance - current_position

        # 如果油量不足以到达下一个加油站
        if current_fuel < distance_to_station:
            return "Impossible"

        # 更新油量（走过这一段距离）
        current_fuel -= distance_to_station
        current_position = station_distance

        # 计算到终点时还需要预留 200L，判断是否需要加油
        if distance - current_position <= current_fuel - 200:
            return total_cost

        cheapest_price = min(price_list[i:])

        # 如果加油不用花钱，则总消费为0
        if cheapest_price == 0:
            return 0
        # 如果当前加油站最便宜，则在当前加油站加油
        if station_price <= cheapest_price:
            # 如果距离终点超出我们行驶的最远距离
            if 200 + (distance - current_position) >= 400:
                # 我们给油箱加满
                fuel_needed = max_fuel_capacity - current_fuel
                total_cost += fuel_needed * station_price
                current_fuel += fuel_needed
            else:
                # 否则刚好跑完全程即可
                fuel_needed = 200 + (distance - current_position) - current_fuel

        # 如果当前加油站价格不是最低的，我们尽量去最便宜的加油站加油
        if station_price > cheapest_price:
            cheapest_station = gas_stations[price_list.index(cheapest_price)]
            # 如果当前所剩燃油足以让我们跑到最便宜的加油站，我们则不需要加油
            if current_fuel >= cheapest_station[0] - current_position:
                fuel_needed = 0
                total_cost += fuel_needed * station_price
                current_fuel += fuel_needed
            # 如果当前所剩燃油不足以让我们跑到最便宜的加油站，我们则需要加油
            else:
                # 如果我们当前加油站油费小于下一站，我们在当前加油站加油
                if station_price <= gas_stations[i + 1][1]:
                    # 如果距离最便宜的加油站的距离超出我行驶的最远距离，则加满
                    if cheapest_station[0] - station_distance >= 400:
                        fuel_needed = max_fuel_capacity - current_fuel
                        total_cost += fuel_needed * station_price
                        current_fuel += fuel_needed
                    # 如果距离最便宜的加油站的距离不超出我行驶的最远距离，则加油到足以到最便宜的加油站即可
                    else:
                        fuel_needed = cheapest_station[0] - station_distance - current_fuel
                        total_cost += fuel_needed * station_price
                        current_fuel += fuel_needed
                # 如果我们当前加油站油费大于下一站，我们在下一个加油站加油
                else:
                    fuel_needed = 0
                    total_cost += fuel_needed * station_price
                    current_fuel += fuel_needed

    # 最后检查能否从最后一个加油站到达终点，并留有200L
    if current_fuel >= distance - current_position + 200:
        return total_cost
    else:
        return "Impossible"


if __name__ == "__main__":
    #  You can add more test cases here
    gas_stations1 = [(100, 1), (200, 30), (400, 40), (300, 20)]
    gas_stations2 = [(100, 999), (150, 888), (200, 777), (300, 999), (400, 1009), (450, 1019), (500, 1399)]
    gas_stations3 = [(101, 12), (100, 100), (102, 1)]
    gas_stations4 = [(34, 1), (105, 9), (9, 10), (134, 66), (215, 90), (999, 1999), (49, 0), (10, 1999), (200, 2),
                     (300, 500), (12, 34), (1, 23), (46, 20), (80, 12), (1, 1999), (90, 33), (101, 23), (34, 88),
                     (103, 0), (1, 1)]

    print(solution(500, 4, gas_stations1) == 4300)
    print(solution(500, 7, gas_stations2) == 410700)
    print(solution(500, 3, gas_stations3) == "Impossible")
    print(solution(100, 20, gas_stations4) == 0)
    print(solution(100, 0, []) == "Impossible")
